package cn.tedu.charging.order.timer;

import cn.tedu.charging.common.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.common.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.common.protocol.WebSocketMessage;
import cn.tedu.charging.order.mapper.BillExceptionMapper;
import cn.tedu.charging.order.mapper.BillSuccessMapper;
import cn.tedu.charging.order.points.WsServerPoint;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Wait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 检查某些订单是否有结束
 */
@Component
@Slf4j
public class OrderExceptionTimer {
    @Autowired
    private BillSuccessMapper billSuccessMapper;
    @Autowired
    private BillExceptionMapper billExceptionMapper;
    @Autowired
    private WsServerPoint wsServerPoint;
    @XxlJob("order-status-check")
    public void checkOrderStatus() {
        //1.拿到任务参数 订单编号
        String billId= XxlJobContext.getXxlJobContext().getJobParam();
        log.info("检查订单状态是否结束,billId:{}",billId);
        //2.订单编号查询成功订单是否存在
        ChargingBillSuccessPO success=getSuccessByBillId(billId);
        //判断异常 存在success且状态值为1
        if (success!=null&&success.getBillStatus()==1){
            log.debug("订单异常结束,billId:{}",billId);
            //3.创建异常写入数据库
            saveExceptionBill(success);
            //4.修改订单状态为3 异常结束
            updateBillStatusException(success);
            //5. 通知用户拔枪走人
            WebSocketMessage<String> message=new WebSocketMessage<>();
            message.setState(1);
            message.setMessage("您的订单已异常结束,请联系管理员");
            message.setData("您的订单已异常结束,请结账走人");
            wsServerPoint.sendMsg2User(success.getUserId(),message);
        }else{
            log.debug("订单正常结束,billId:{}",billId);
        }
    }

    private void updateBillStatusException(ChargingBillSuccessPO success) {
        //update charging_bill_success set bill_status=3 where id=#{}
        ChargingBillSuccessPO poParam=new ChargingBillSuccessPO();
        poParam.setId(success.getId());
        poParam.setBillStatus(3);
        billSuccessMapper.updateById(poParam);
    }

    private void saveExceptionBill(ChargingBillSuccessPO success) {
        //1.组织一个异常po对象
        ChargingBillExceptionPO exception=new ChargingBillExceptionPO();
        exception.setBillId(success.getBillId());
        exception.setCreateTime(new Date());
        exception.setDeleted(0);
        //2.写入数据
        billExceptionMapper.insert(exception);
    }

    private ChargingBillSuccessPO getSuccessByBillId(String billId) {
        //select * from charging_bill_success where bill_id=#{}
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bill_id",billId);
        return billSuccessMapper.selectOne(queryWrapper);
    }
}
